package cn.cool.domain.activity.service.trial.node;

import cn.cool.domain.activity.model.entity.MarketProductEntity;
import cn.cool.domain.activity.model.entity.TrialBalanceEntity;
import cn.cool.domain.activity.service.trial.AbstractGroupBuyMarketSupport;
import cn.cool.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import cn.cool.types.design.framework.tree.StrategyHandler;
import cn.cool.types.enums.ResponseCode;
import cn.cool.types.exception.AppException;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class RootNode extends AbstractGroupBuyMarketSupport<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> {
    @Resource
    private SwitchNode switchNode;

    @Override
    public TrialBalanceEntity doApply(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        log.info("优惠试算 - 根节点： requestParameter = {}, dynamicContext = {}", JSONUtil.toJsonStr(requestParameter), JSONUtil.toJsonStr(dynamicContext));
        if (StrUtil.isBlank(requestParameter.getUserId()) || StrUtil.isBlank(requestParameter.getGoodsId()) ||
                StrUtil.isBlank(requestParameter.getSource()) || StrUtil.isBlank(requestParameter.getChannel())) {
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }
        // 设置 上下文内容，传递给下一个节点就ok
        return router(requestParameter, dynamicContext);
    }

    @Override
    public StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> get(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        return switchNode;
    }
}
